﻿namespace Renci.iRods.Studio.Modules.Explorer.Models
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Renci.iRods.Data;
    using System.ComponentModel;
    using Renci.iRods.Studio.Common;

    public class FileModel : ExplorerItemModel
    {
        private IRodsFile _file;

        public override string TypeName
        {
            get { return "File"; }
        }

        public override bool HasChildren
        {
            get { return false; }
        }

        [DisplayName("Collection ID"), ReadOnly(true), Browsable(true)]
        public long CollectionID { get; private set; }

        [DisplayName("Collection Name"), ReadOnly(true), Browsable(true)]
        public string CollectionName { get; private set; }

        [DisplayName("Replication Number"), ReadOnly(true), Browsable(true)]
        public int ReplNum { get; private set; }

        [DisplayName("Version"), ReadOnly(true), Browsable(true)]
        public string Version { get; private set; }

        [DisplayName("Size"), ReadOnly(true), Browsable(true)]
        public long Size { get; private set; }

        [DisplayName("Resource Group Name"), ReadOnly(true), Browsable(true)]
        public string ResourceGroupName { get; private set; }

        [DisplayName("Resource Name"), ReadOnly(true), Browsable(true)]
        public string ResourceName { get; private set; }

        [DisplayName("Path"), ReadOnly(true), Browsable(true)]
        public string Path { get; private set; }

        [DisplayName("Replication Status"), ReadOnly(true), Browsable(true)]
        public string ReplicationStatus { get; private set; }

        [DisplayName("Status"), ReadOnly(true), Browsable(true)]
        public string Status { get; private set; }

        [DisplayName("Checksum"), ReadOnly(true), Browsable(true)]
        public string Checksum { get; private set; }

        [DisplayName("Expire"), ReadOnly(true), Browsable(true)]
        public string Expire { get; private set; }

        [DisplayName("Map ID"), ReadOnly(true), Browsable(true)]
        public long MapID { get; private set; }

        [DisplayName("Mode"), ReadOnly(true), Browsable(true)]
        public string Mode { get; private set; }

        [DisplayName("Is Trash"), ReadOnly(true), Browsable(true)]
        public bool IsTrash { get; private set; }

        private IEnumerable<MetadataModel> _metadata;
        public override IEnumerable<MetadataModel> Metadata
        {
            get
            {
                if (this._metadata == null)
                {
                    this._metadata = from m in this._file.MetaData
                                     select new MetadataModel(m.ID, m.Namespace, m.Name, m.Value, m.Unit);
                }

                return this._metadata;
            }
        }

        internal FileModel(IRodsConnection connection, IRodsFile file)
            : base(connection)
        {
            this._file = file;
            this.ID = this._file.ID;
            this.Name = this._file.Name;
            this.FullName = this._file.FullName;
            this.OwnerName = this._file.OwnerName;
            this.OwnerZone = this._file.OwnerZone;
            this.CreationTime = this._file.CreationTime;
            this.LastWriteTime = this._file.LastWriteTime;
            this.CollectionID = this._file.CollectionID;
            this.CollectionName = this._file.CollectionName;
            this.ReplNum = this._file.ReplNum;
            this.Version = this._file.Version;
            this.Size = this._file.Size;
            this.ResourceGroupName = this._file.ResourceGroupName;
            this.ResourceName = this._file.ResourceName;
            this.Path = this._file.Path;
            this.ReplicationStatus = this._file.ReplicationStatus;
            this.Status = this._file.Status;
            this.Checksum = this._file.Checksum;
            this.Expire = this._file.Expire;
            this.MapID = this._file.MapID;
            this.Mode = this._file.Mode;
            this.IsTrash = this._file.IsTrash;
        }
    }
}
